var dataGrid;
var organizationTree;
var pathName = window.document.location.pathname;
//获取带"/"的项目名
var ctx = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
	$(function() {
		if(resCode=='1'){
			if(USER){
		/**加载用户列表*/
		dataGrid = $('#dataGrid').datagrid({
			url : ctx+ '/userManger/staffListDate',
			striped : true,
			rownumbers : true,
			pagination : true,
			singleSelect : true,
			idField : 'userId',
			sortName : 'userId',
			sortOrder : 'asc',
			pageSize : 10,
			pageNumber : 1,
			pageList : [ 10, 20, 30, 40, 50, 100, 200, 300, 400, 500 ],
			frozenColumns : [ [{
				width : '150',
				title : '用户名',
				field : 'userName',
				sortable : true
			} , 
			{
				width : '150',
				title : '账号',
				field : 'userCode',
				sortable : true
			} ,{
				width : '100',
				title : '性别',
				field : 'sex',
				formatter : function(value, row, index) {
						var str='';
						if(value=='1'){
							str='男';					
						}else{
							str='女';
						}
						  return str; 
							
						}
			} ,{
				width : '150',
				title : '邮箱',
				field : 'email'
			} ,
			{
				width : '150',
				title : '电话',
				field : 'phone'
			}, 
			{
				width : '200',
				title : '有效期',
				field : 'validDate',
				formatter : function(value, row, index) {
					 	var date = new Date(value);
						if(value == "" || value == null)
						{	return "";
						}
						else
						{
							var y = date.getFullYear();
							var m = date.getMonth()+1;
							var d = date.getDate();
							return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
						}
				}
			} ,
			{
				width : '100',
				title : '锁定状态',
				field : 'deleteState',
				formatter : function(value, row, index) {
					var str='';
					if(value=='0'){
						str='正常';					
					}else{
						str='锁定';
					}
					  return str; 
						
					}
				} , {
				field : 'action',
				title : '操作',
				width : 150,
				formatter : function(value, row, index) {
					var str = '&nbsp;';
					if(USER_UNLOCK){
						if(row.deleteState =='0'){
							str += $.formatString('<a href="javascript:void(0)" onclick="deleteFun(\'{0}\');" >禁用</a>', row.userId);
						}
					}
                    if(USER_DELETE){
                    	if(row.deleteState =='1'){
                    		str += $.formatString('<a href="javascript:void(0)" onclick="updateFun(\'{0}\');" >恢复</a>', row.userId);
						}
					}
					
				if(USER_EDIT){
								str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
								str += $.formatString('<a href="javascript:void(0)"  style="text-decoration:none;" onclick="editFun(\'{0}\');" >编辑</a>', row.userId);
					}
					
							return str;
				}
			} ] ],
			onLoadError:function(e){
				if(e.readyState==4){
					parent.$.messager.alert('错误',"获取用户数据失败，服务器错误！", 'error');
				}
				
			},
			toolbar : '#toolbar'
		});
			}else{
				parent.$.messager.confirm('询问', '当前登录用户没有操作此功能的权限，重新登录！！',
						function(b) {
							if (b) {
								parent.window.location.href = ctx+'/login';
							}
						});
			}
	}else if(resCode=='0'){
		/**登录失效*/
		parent.$.messager.confirm('询问', '您还未登录或登录超时！请重新登录？', function(b) {
			if (b) {
				parent.window.location.href=ctx+'/login';
			}
		});
	}		

		getChannel();
	});
	
	
	function editFun(id) {
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.modalDialog({
			title : '编辑',
			width : 400,
			height : 200,
			href : ctx+'/userManger/edit?id=' + id,
			buttons : [ {
				text : '编辑',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#demoEditForm');
					f.submit();
				}
			} ]
		});
	}
	
	function deleteFun(id) {
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		
		jQuery.ajax({
			url:ctx+'/userManger/delete',
			type: "POST",
			async: "false",
			timeout: 30000,
			data: {"id":id},
			success: function(ret) {
				if(ret==0){
					parent.$.messager.alert('提示',"删除失败", 'error');
					//请求父页面关闭
					searchCook();
				}
				if(ret==1){
					parent.$.messager.alert('错误', "删除成功", 'info');
					//请求父页面关闭
					searchCook();
				}
				
			},
			error: function() {
				parent.$.messager.alert('错误', "数据异常，添加失败", 'error');
				//请求父页面关闭
				//add_close();
			}
		});	
	}
	
	function updateFun(id) {
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
       jQuery.ajax({
			url:ctx+'/userManger/updateFun',
			type: "POST",
			async: "false",
			timeout: 30000,
			data: {"id":id},
			success: function(ret) {
				if(ret==0){
					parent.$.messager.alert('提示',"恢复失败", 'error');
					//请求父页面关闭
					searchCook();
				}
				if(ret==1){
					parent.show("恢复成功","提示");
					//请求父页面关闭
					searchCook();
				}
				
			},
			error: function() {
				parent.$.messager.alert('错误', "数据异常，添加失败", 'error');
				//请求父页面关闭
				
			}
		});	
	}
	/*键盘监听事件*/
	document.onkeydown = function(event) {
		if (event && event.keyCode == 13) {//enter 333键 
			location.reload();
			return false;
		}
	};
	
	/* 提交查询表单* */
	function searchCook() {
		dataGrid.datagrid('load', $.serializeObject($('#searchForm')));
	}
	function getChannel(){
		$.ajax({   
			url:ctx+'/userManger/getSysCode', 
			type:'post',
			async : false, //默认为true 异步  
			dataType:'json',
			error:function(){
				alert('服务器异常！');  
			},   
			success:function(data){ 
				var chDate = eval(data);
				if(null!=chDate && chDate.length>0 ){
					for(var i=0;i<chDate.length;i++){
						var str = "<option value='"+chDate[i].sysCode+"'>"+chDate[i].sysName+"</option>";
						$("#sysCode").append(str);
					}
				}
			
			}
		});
		
	}
	